package com.haoshuang.zhouzhoubang.ui.activity

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import com.haoshuang.zhouzhoubang.databinding.ActivityManagerMemberBinding
import com.haoshuang.zhouzhoubang.model.bean.GroupBean
import com.haoshuang.zhouzhoubang.ui.adapter.Meger1Adapter
import com.haoshuang.zhouzhoubang.ui.dialog.Ts101Dialog
import com.haoshuang.zhouzhoubang.ui.vm.ManagerMemberViewModel
import com.jeremyliao.liveeventbus.LiveEventBus
import com.lnkj.jialubao.utils.AccountUtils
import com.lxj.xpopup.XPopup
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.ktx.getString
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.vertical


/**
 *  成员管理
 */
class ManagerMemberActivity :
    BaseVMActivity<ManagerMemberViewModel, ActivityManagerMemberBinding>() {

    companion object {
        fun launch(context: Context) {
            context.startPage<ManagerMemberActivity>()
        }
    }
    private var group_status = 0
    private var user_id = ""

    private val data = arrayListOf<GroupBean.AllPeople>()
    private val adapter = Meger1Adapter(data)
    override fun initView(savedInstanceState: Bundle?) {

        group_status = intent.getIntExtra("group_status", 0)
        user_id = intent.getString("user_id")

        binding.titleBar.title = "管理成员"
        binding.titleBar.leftImageView().click { finish() }

        binding.recyclerView.vertical()
        binding.recyclerView.adapter = adapter

        adapter.setOnItemClickListener { _, view, position ->
            val item = adapter.getItem(position)
//            startPage<PersonalHomeActivity>("user_id" to item.uid)
            if (data[position].isCHeck == 0){
                data[position].isCHeck = 1
            }else{
                data[position].isCHeck = 0
            }
            adapter.notifyDataSetChanged()
        }

        binding.tvDel.clickWithTrigger {
            val list = data.filter { it.isCHeck == 1 }
            if (list.isEmpty()){
                toast("请选择删除成员")
                return@clickWithTrigger
            }
            val mine = data.firstOrNull { it.uid.toString() == AccountUtils.user?.uid }
            if (mine != null){
                toast("店长账号不能删除")
                return@clickWithTrigger
            }
            val ids = list.joinToString(","){ it.uid.toString() }
            XPopup.Builder(this)
                .asCustom(
                    Ts101Dialog(
                        this
                    ) {
                        vm.delMember(
                            "to_user_id" to ids
                        )
                    })
                .show()
        }
    }

    override fun initData() {
        vm.getTeamMemberList()
    }

    override fun startObserve() {
        vm.delMemberData.observeState(this){
            onLoading {showLoading()}
            onError {code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess{
                dismissLoading()
                vm.getTeamMemberList()
            }
        }

        vm.getTeamMemberListData.observeState(this){
            onLoading {showLoading()}
            onError {code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                data.clear()
                data.addAll(it?: emptyList())
                data.map { it.isCHeck2 = 1 }

                adapter.notifyDataSetChanged()
            }
        }
    }

}